<template>
  <div class="data-statistics-container">
    <div class="statistics-wrapper">
      <!-- 页面头部 -->
      <div class="page-header">
        <div class="header-icon">
          <Icon icon="ep:data-analysis" />
        </div>
        <div class="header-content">
          <h3 class="page-title">数据统计</h3>
          <p class="page-subtitle">查看企业微信相关数据统计信息</p>
        </div>
      </div>

      <!-- 标签页导航 -->
      <div class="tab-navigation">
        <div class="tab-container">
          <div
            class="tab-item"
            v-for="item in menuList"
            :key="item.id"
            :class="{ 'tab-active': menuIndex == item.id }"
            @click="changeItem(item.id)"
          >
            <Icon :icon="item.id === 1 ? 'ep:user' : 'ep:chat-dot-round'" class="tab-icon" />
            <span class="tab-text">{{ item.name }}</span>
          </div>
        </div>
      </div>

      <!-- 搜索区域 -->
      <div class="search-section">
        <div class="search-container">
          <el-input v-model="searchVal" clearable placeholder="请输入名称搜索" class="search-input">
            <template #prefix>
              <Icon icon="ep:search" class="search-prefix-icon" />
            </template>
            <template #append>
              <el-button :icon="Search" @click="searchBtn" class="search-btn" />
            </template>
          </el-input>
        </div>
      </div>

      <!-- 内容区域 -->
      <div class="content-section">
        <div class="statistics-content">
          <MenuIndex
            :list="list"
            :menuIndex="menuIndex"
            :key="mathRandom"
            @setCardList="setCardList"
            :params="params"
            :totals="totals"
            @getList="getList"
            class="menu-index-component"
          />
          <menuContent :cardList="cardList" :menuIndex="menuIndex" class="menu-content-component" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import * as chatGroupApi from '@/api/wecom/chatgroup/index'
import * as userApi from '@/api/wecom/user/index'
import { dateFormatter } from '@/utils/formatTime'
import { Search } from '@element-plus/icons-vue'
import MenuIndex from './components/menuIndex.vue'
import menuContent from './components/menuContent.vue'

const menuList = [
  {
    name: '员工客户联系统计',
    id: 1
  },
  {
    name: '员工客户群统计',
    id: 2
  }
]

const totals = ref(0)
const params = ref({
  pageNo: 1,
  pageSize: 8
})
const list = ref([])
const cardList = ref([])
const mathRandom = ref(Math.random())
const menuIndex = ref(1)
const searchVal = ref('')

// 搜索按钮
const searchBtn = () => {
  params.value.pageNo = 1
  getCustomerList()
}

// 分页
const getList = (val) => {
  params.value.pageNo = val.page
  getCustomerList()
}

onMounted(() => {
  getCustomerList()
})

const changeItem = (val) => {
  menuIndex.value = val
}

//员工列表
const getCustomerList = async () => {
  const result = await userApi.getPageWeUserList({ nickname: searchVal.value, ...params.value })
  list.value = result.list
  totals.value = result.total
  mathRandom.value = Math.random()
}

const setCardList = (value) => {
  cardList.value = value
}
</script>

<style scoped lang="scss">
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.data-statistics-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);

  @media (max-width: 768px) {
    padding: 8px;
  }
}

.statistics-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;
  background: white;
  height: 100vh;
  display: flex;
  flex-direction: column;

  @media (max-width: 768px) {
    height: calc(100vh - 100px);
  }

  :deep(.el-card__body) {
    padding: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
}

/* 页面头部 */
.page-header {
  display: flex;
  align-items: center;
  padding: 12px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;
  flex-shrink: 0;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
  margin-left: 16px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.page-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 标签页导航 */
.tab-navigation {
  padding: 24px 24px 0;
  background: white;
  flex-shrink: 0;

  @media (max-width: 768px) {
    padding: 16px 16px 0;
  }
}

.tab-container {
  display: flex;
  justify-content: center;
  background: #f8f9ff;
  border-radius: $border-radius-small;
  padding: 4px;
  border: 1px solid $border-light;

  @media (max-width: 768px) {
    flex-direction: row;
    overflow-x: auto;
    justify-content: flex-start;
  }
}

.tab-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 500;
  color: $text-secondary;
  background: transparent;
  border: 1px solid transparent;
  flex: 1;
  justify-content: center;
  min-width: 160px;
  white-space: nowrap;

  @media (max-width: 768px) {
    padding: 10px 16px;
    font-size: 13px;
    min-width: 140px;
    flex-shrink: 0;
  }

  @media (max-width: 480px) {
    padding: 8px 12px;
    font-size: 12px;
    min-width: 120px;
  }

  &:hover {
    color: $primary-color;
    background: rgba(0, 82, 204, 0.05);
  }

  &.tab-active {
    background: $primary-color;
    color: white;
    border-color: $primary-color;
    box-shadow: 0 2px 8px rgba(0, 82, 204, 0.3);
  }
}

.tab-icon {
  font-size: 16px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.tab-text {
  @media (max-width: 480px) {
    font-size: 11px;
  }
}

/* 搜索区域 */
.search-section {
  padding: 20px 24px;
  background: white;
  border-bottom: 1px solid $border-light;
  flex-shrink: 0;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.search-container {
  max-width: 400px;

  @media (max-width: 768px) {
    max-width: 100%;
  }
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input-group__append) {
    border-radius: 0 $border-radius-small $border-radius-small 0;
    border-left: none;
    background: $primary-color;
    border-color: $primary-color;

    .search-btn {
      background: transparent;
      border: none;
      color: white;
      padding: 0 12px;

      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }
    }
  }
}

.search-prefix-icon {
  color: $text-placeholder;
  font-size: 14px;
}

/* 内容区域 */
.content-section {
  padding: 12px 24px;
  background: white;
  overflow: hidden;

  @media (max-width: 768px) {
    padding: 0 16px 16px;
  }
}

.statistics-content {
  height: 100%;
  display: flex;
  gap: 16px;
  overflow: hidden;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }
}

.menu-index-component {
  flex-shrink: 0;
  width: 300px;
  height: 100%;
  border-radius: $border-radius-small;
  border: 1px solid $border-light;
  box-shadow: $shadow-base;
  overflow: hidden;

  @media (max-width: 768px) {
    width: 100%;
    height: 300px;
    flex-shrink: 1;
  }

  @media (max-width: 480px) {
    height: 250px;
  }
}

.menu-content-component {
  flex: 1;
  height: 100%;
  border-radius: $border-radius-small;
  border: 1px solid $border-light;
  box-shadow: $shadow-base;
  overflow: hidden;

  @media (max-width: 768px) {
    height: calc(100% - 312px);
    min-height: 300px;
  }

  @media (max-width: 480px) {
    height: calc(100% - 262px);
    min-height: 250px;
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .data-statistics-container {
    padding: 4px;
  }

  .tab-navigation,
  .search-section,
  .content-section {
    padding-left: 12px;
    padding-right: 12px;
  }

  .page-header {
    padding: 12px;
  }

  .tab-item {
    gap: 4px;
  }

  .statistics-content {
    gap: 8px;
  }
}

/* 滚动条美化 */
.tab-container {
  &::-webkit-scrollbar {
    height: 4px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 2px;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .data-statistics-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .statistics-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .tab-navigation,
  .search-section,
  .content-section {
    background: #374151;
  }

  .page-title {
    color: #f9fafb;
  }

  .tab-container {
    background: #4b5563;
    border-color: #6b7280;
  }

  .tab-item {
    color: #d1d5db;

    &:hover {
      color: #60a5fa;
      background: rgba(96, 165, 250, 0.1);
    }

    &.tab-active {
      background: #60a5fa;
      color: white;
      border-color: #60a5fa;
    }
  }

  .menu-index-component,
  .menu-content-component {
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }
}
</style>
